/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import model.MusicGroup;

/**
 *
 * @author Eleftherios
 */
public class MusicGroupObj {
    private Integer musicGroupId;
    private String name;
    private Date formationDate;
    
    public MusicGroupObj() {
    }

    public MusicGroupObj(Integer musicGroupId) {
        this.musicGroupId = musicGroupId;
    }

    public MusicGroupObj(Integer musicGroupId, String name, Date formationDate) {
        this.musicGroupId = musicGroupId;
        this.name = name;
        this.formationDate = formationDate;
    }

    public Integer getMusicGroupId() {
        return musicGroupId;
    }

    public void setMusicGroupId(Integer musicGroupId) {
        this.musicGroupId = musicGroupId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getFormationDate() {
        return formationDate;
    }

    public void setFormationDate(Date formationDate) {
        this.formationDate = formationDate;
    } 
    
      public void bindList(javax.swing.JTable JT)   {
       MusicGroup MyClass = new MusicGroup();
       List<MusicGroup> list = MyClass.read(Boolean.TRUE);
              
       DefaultTableModel model = (DefaultTableModel) JT.getModel(); // ανάκληση του model απο το αντικείμενο του πίνακα                     
       for (int i=model.getColumnCount()-1; i>-1; i--) {  // καθάρισε τις Στήλες 
          TableColumn col = JT.getColumnModel().getColumn(i); 
           JT.removeColumn(col);
           model.setColumnCount(model.getColumnCount()-1);
           JT.revalidate(); 
       }
       // Ορίζω νέες στήλες 
       model.addColumn("Κωδικός");
       model.addColumn("Όνομα");
             
       for (int c = 0; c < JT.getColumnCount(); c++)    // ο πίνακας γίνεται read only
       {
               Class<?> col_class = JT.getColumnClass(c);
               JT.setDefaultEditor(col_class, null);        
       }      
       for (int i=model.getRowCount()-1; i>-1; i--){ model.removeRow(i);} // καθάρισε τις γραμμές   
       // μεταφορά των δεδομένων στον πίνακα
       for (int i=0; i < list.size(); i++)
         {
             model.addRow(new Object[]{list.get(i).getMusicGroupId(), list.get(i).getName()});
          }
    }
     
    public void Read() {
        MusicGroup MyClass = new MusicGroup();
        MyClass.setMusicGroupId(musicGroupId);
        MyClass.read(Boolean.FALSE);    
        musicGroupId=MyClass.getMusicGroupId();   
        name=MyClass.getName();
        formationDate=MyClass.getFormationDate();
    }
      
    public long update(){
        MusicGroup MyClass = new MusicGroup();
        MyClass.setMusicGroupId(musicGroupId);
        MyClass.setName(name);
        MyClass.setFormationDate(formationDate);
        return MyClass.update();
    }
    
    public long AddNew(){
        MusicGroup MyClass = new MusicGroup();
        MyClass.setMusicGroupId(musicGroupId);
        MyClass.setName(name);
        MyClass.setFormationDate(formationDate);
        return MyClass.insert();
    }
    
    public long delete(){
        MusicGroup MyClass = new MusicGroup();
        MyClass.setMusicGroupId(musicGroupId);
        return MyClass.delete();
    }
    
    public void test(){
        // MusicGroup MyClass = new MusicGroup();
             
    }
       
}
